package com.welink.cmp.adapter.api.metadata;

import com.welink.base.catchlog.CatchAndLog;
import com.welink.cmp.dto.IdQuery;
import com.welink.cmp.dto.ErrorCode;
import com.welink.cmp.dto.metadata.region.RegionAddCmd;
import com.welink.cmp.dto.metadata.region.RegionCO;
import com.welink.cmp.dto.metadata.region.RegionPageQry;
import com.welink.cmp.dto.metadata.region.RegionUpdCmd;
import com.welink.cmp.meatdata.service.RegionAppService;
import com.welink.common.dto.MultiResponse;
import com.welink.common.dto.PageResponse;
import com.welink.common.dto.Response;
import com.welink.common.dto.SingleResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 区域服务接口
 *
 * @create liyanwei
 */
@RestController
@RequestMapping("/api/region")
@CatchAndLog
public class RegionServiceApi {

    @Autowired
    private RegionAppService regionAppService;

    /**
     * [新增] 新增区域
     * @param addCmd addCmd
     * @return Id
     */
    @PostMapping("/add")
    public SingleResponse<Long> addRegion(@RequestBody RegionAddCmd addCmd){
        final Long id = regionAppService.addRegion(addCmd);
        return SingleResponse.of(id);
    }

    /**
     * [修改] 修改区域名称
     * @param updCmd updCmd
     * @return code
     */
    @PostMapping("/modify")
    public Response modifyRegion(@RequestBody RegionUpdCmd updCmd){
        final Boolean b = regionAppService.updateRegion(updCmd);
        if (!b){
            return Response.buildFailure(ErrorCode.CommonError.E_Service_Inner_Exception,"更新区域信息失败");
        }
        return Response.buildSuccess();
    }
    /**
     * [ID查询] 单一查询
     * @param idQuery Id
     * @return
     */
    @GetMapping("/item")
    public SingleResponse<RegionCO> queryRegionById(@RequestBody IdQuery idQuery){
        final RegionCO regionCo = regionAppService.getRegionById(idQuery.getId());
        return SingleResponse.of(regionCo);
    }

    /**
     * [列表查询] 查询全部区域
     * @return 区域列表
     */
    @GetMapping("/all")
    public MultiResponse<RegionCO> getAllRegion(){
        List<RegionCO> list;
        list = regionAppService.getAllRegion();
        return MultiResponse.of(list);
    }

    /**
     * [分页查询] 分页查询区域
     * @param pageQry 页面查询
     * @return 分页查询
     */
    @GetMapping("/page")
    public PageResponse<RegionCO> pageQueryRegion(@RequestBody RegionPageQry pageQry){
        return regionAppService.regionPageQuery(pageQry);
    }
}
